package vtt.android.tinthethao_ota.fragments;

import vtt.android.tinthethao_ota.R;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.google.android.gms.ads.AdView;

import fr.castorflex.android.smoothprogressbar.SmoothProgressBar;

/**
 * A placeholder fragment containing a simple view.
 */
public class KeoDaBanhFragment extends BaseWebFragment {
	
	private View mLPSRootView;
	private String fontSize;
	
	/**
	 * Returns a new instance of this fragment for the given section number.
	 */
	public static KeoDaBanhFragment newInstance(int sectionNumber) {
		KeoDaBanhFragment fragment = new KeoDaBanhFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		fragment.setArguments(args);
		return fragment;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		
		fontSize = (mParentAct.getResources().getDisplayMetrics().density  * 120) + "%";
		log.d("VTT>>> fontSize " + fontSize);
		
		mLPSRootView = inflater.inflate(R.layout.fragment_cm_webview, container, false);
		mProgressBar = (SmoothProgressBar) mLPSRootView.findViewById(R.id.progressbar_loading);

		// load admob
		AdView adView = (AdView) mLPSRootView.findViewById(R.id.adView);
		loadAdmob(adView);
				
		// init webview
		mWebView = (WebView) mLPSRootView.findViewById(R.id.webView1);
		// init common webview
		initCommonWebview();
		
		mWebView.setWebViewClient(new WebClient());
		// init custom config
		mWebView.setLongClickable(false);
		mWebView.getSettings().setJavaScriptEnabled(false);
		if (Build.VERSION.SDK_INT >= 11){
            mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        }
		
		// If there is a previous instance restore it in the webview
	    mWebView.loadUrl(getString(R.string.url_keodabanh));	
	    
		return mLPSRootView;
	}
	
	class WebClient extends WebViewClient {

		@Override
		public void onPageStarted(WebView view, String url, Bitmap favicon) {
			mProgressBar.progressiveStart();
			/*if (mWebView != null) {
			    mWebView.setVisibility(View.INVISIBLE);
			}*/
			super.onPageStarted(view, url, favicon);
		}

		@Override
		public void onPageFinished(WebView view, String url) {
			
			/*StringBuffer injectScript = new StringBuffer();
			injectScript.append("javascript: ");
			injectScript.append("document.getElementById('div_content1').innerHTML = ''; ");
			injectScript.append("document.getElementById('div_content2').innerHTML = ''; ");
			injectScript.append("document.getElementById('div_content3').innerHTML = ''; ");
			
			injectScript.append("document.getElementById('div_footer1').innerHTML = ''; ");
			injectScript.append("document.getElementById('div_footer2').innerHTML = ''; ");
			injectScript.append("document.getElementById('div_footer3').innerHTML = ''; ");
			injectScript.append("document.getElementById('div_footer4').innerHTML = ''; ");
			injectScript.append("document.getElementById('div_footer5').innerHTML = ''; ");
			// hide ads menu
			injectScript.append("document.getElementById('ads_topmenu_1').innerHTML = ''; ");
			injectScript.append("document.getElementById('ads_topmenu_2').innerHTML = ''; ");
			// remove all iframe
			injectScript.append("window.setTimeout(function(){ var iframes = document.getElementsByTagName('iframe'); for (var i = 0; i < iframes.length; i++) { iframes[i].parentNode.removeChild(iframes[i]); } }, 3000);");
			view.loadUrl(injectScript.toString());
			
			// show webview
			try {
			    view.postDelayed(new Runnable() {
			        @Override
			        public void run() {
			            if(mWebView != null){
			                mWebView.setVisibility(View.VISIBLE);
			            }
			        }
			    }, 1000);
            } catch (Exception e) {
                
            }*/
			super.onPageFinished(view, url);
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			log.d("VTT>>>shouldOverrideUrlLoading url " + url);
			// prevent user install another app
            if(url.contains("play.google.com/store/apps") || url.contains("market://details?id=") || url.endsWith(".apk")){
                return true;
            }
			return super.shouldOverrideUrlLoading(view, url);
		}
	}
}